// 配置 axios
import axios from 'axios'

let baseURL = 'http://localhost:6290'
const service = axios.create({
    baseURL: baseURL,
    timeout: 10 * 1000,
});


service.interceptors.request.use(config => {
    // 这里可以设置token: config!.headers!.Authorization = token
    return config;
});
service.interceptors.response.use(response => {
    let status = response.status;
    // 判断http的状态码是否正常
    if (status < 400) {
        return response.data;
    } else if (status >= 500) {
        return Promise.reject(new Error(`Unexpected status code: ${status}`));
    } else {
        return response.data;
    }
});

const request = {
    baseURL: baseURL,
    get: function (url, queryStringParams) {
        let axiosRequestConfig = {
            method: 'get',
            params: queryStringParams
        }
        return service.get(url, axiosRequestConfig);
    },
    post: function (url, payloadData, queryStringParams) {
        let axiosRequestConfig = {
            method: 'post',
            data: payloadData,
            params: queryStringParams,
            headers: {'Content-Type': 'application/json'}
        }
        return service.post(url, payloadData, axiosRequestConfig);
    },
    postForm: function (url, payloadData, queryStringParams) {
        let axiosRequestConfig = {
            method: 'post',
            data: payloadData,
            params: queryStringParams,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }
        return service.post(url, payloadData, axiosRequestConfig);
    }
};
export default request;